package flc.ast.activity;

import static com.stark.imgocr.api.VerifyMethod.APPCODE;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgocr.ImgOcrEntry;
import com.stark.imgocr.api.Config;
import com.stark.imgocr.api.IRetCallback;
import com.stark.imgocr.api.bean.OcrRetBean;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.databinding.ActivityMyAlbumBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class MyAlbumActivity extends BaseAc<ActivityMyAlbumBinding> {

    public static int type;
    private int oldPosition;
    private String selPath = "";
    private SelPictureAdapter pictureAdapter;
    private List<String> listSel = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_my_album;
    }

    @Override
    protected void initView() {
        mDataBinding.ivMyAlbumBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.llImport.setOnClickListener(this);

        //相册图片
        pictureAdapter = new SelPictureAdapter();
        mDataBinding.rvPicList.setLayoutManager(new GridLayoutManager(mContext, 3));
        mDataBinding.rvPicList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llImport://导入
                if (TextUtils.isEmpty(selPath) && listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_picture));
                    return;
                }
                if (type == 0) {
                    applyCropImage(ImageUtils.getBitmap(selPath));
                } else if (type == 1) {
                    PictureMagnifyActivity.sImgPath = selPath;
                    startActivity(PictureMagnifyActivity.class);
                    break;
                } else if (type == 2) {
                    PicEditActivity.sImgPath = selPath;
                    startActivity(PicEditActivity.class);
                    break;
                } else if (type == 3) {
                    PuzzleActivity.isFree = true;
                    PuzzleActivity.listPath = listSel;
                    startActivity(PuzzleActivity.class);
                } else if (type == 4) {
                    PuzzleActivity.isFree = false;
                    PuzzleActivity.listPath = listSel;
                    startActivity(PuzzleActivity.class);
                } else if (type == 5) {
                    OriActivity.type = true;
                    OriActivity.listPath = listSel;
                    startActivity(OriActivity.class);
                }else if (type == 6) {
                    OriActivity.type = false;
                    OriActivity.listPath = listSel;
                    startActivity(OriActivity.class);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 0 || type == 1 || type == 2) {
            pictureAdapter.getItem(oldPosition).setChecked(false);
            pictureAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            pictureAdapter.getItem(position).setChecked(true);
            pictureAdapter.notifyItemChanged(position);
            selPath = pictureAdapter.getItem(position).getPath();
            mDataBinding.tvImportNum.setText(getString(R.string.import_num, 1));
        } else {
            if (pictureAdapter.getItem(position).isChecked()) {
                pictureAdapter.getItem(position).setChecked(false);
                pictureAdapter.notifyItemChanged(position);
                listSel.remove(pictureAdapter.getItem(position).getPath());
            } else {
                if (listSel.size() == 9) {
                    ToastUtils.showShort(getString(R.string.max_sel_nine));
                    return;
                }
                pictureAdapter.getItem(position).setChecked(true);
                listSel.add(pictureAdapter.getItem(position).getPath());
            }
            pictureAdapter.notifyDataSetChanged();
            mDataBinding.tvImportNum.setText(getString(R.string.import_multiple_num, listSel.size()));
        }
    }

    @Override
    protected void initData() {
        getPermission();
        if (type == 0 || type == 1 || type == 2) {
            mDataBinding.tvImportNum.setText(getString(R.string.import_num, 0));
        } else {
            mDataBinding.tvImportNum.setText(getString(R.string.import_multiple_num, 0));
        }
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPicList.setVisibility(View.VISIBLE);
                    mDataBinding.tvMyAlbumNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPicList.setVisibility(View.GONE);
                    mDataBinding.tvMyAlbumNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    public void applyCropImage(Bitmap bitmap) {
        showDialog(getString(com.stark.common.res.R.string.handling));
        Config config;
        config = Config.verifyByAppCode(String.valueOf(APPCODE));
        ImgOcrEntry.init(config);
        ImgOcrEntry.getOcrReqManager().urlImages(this, bitmap, new IRetCallback<List<OcrRetBean.Word>>() {
            @Override
            public void onRet(String errCode, String errMsg, @Nullable List<OcrRetBean.Word> wordList) {
                if (wordList == null) {
                    ToastUtils.showShort(errMsg);
                    return;
                }
                StringBuilder stringBuilder = new StringBuilder();
                OcrRetBean.Word word = null;
                for (int i = 0; i < wordList.size(); i++) {
                    word = wordList.get(i);
                    stringBuilder.append(word.getWords());
                    if (i != wordList.size() - 1) {
                        stringBuilder.append("\n");
                    }
                }
                dismissDialog();
                RecResultActivity.sResult = stringBuilder.toString();
                startActivity(RecResultActivity.class);
            }
        });
    }

}